package com.elbis.business.email;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.jboss.logging.Logger;

import com.elbis.util.FileCipherDecipher;

public class EmailSender implements Runnable {

	private String[] to = null;

	private String from = null;

	private String subject = null;

	private String content = null;

	private String host = null;

	private int port;

	private String username = null;

	private String password = null;

	private Session session = null;

	private Properties props = null;

	private Properties emailProperties = null;

	private static final Logger logger = Logger.getLogger(EmailSender.class);

	public EmailSender(String[] to, String subject, String content) {
		this.to = to;
		this.subject = subject;
		this.content = content;
		initProperties();
	}

	private void initProperties() {
		props = new Properties();
		emailProperties = new Properties();
		try {
			InputStream propStream = this.getClass().getClassLoader()
					.getResourceAsStream("email.properties");
			emailProperties.load(propStream);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
			return;
		}
		this.port = Integer.valueOf(emailProperties.getProperty("mail.port"));
		this.from = emailProperties.getProperty("mail.from");
		this.host = emailProperties.getProperty("mail.host");
		this.username = emailProperties.getProperty("email.username");
		props.setProperty(emailProperties.getProperty("mail.host.property"),
				host);
		props.setProperty(emailProperties.getProperty("mail.port.property"),
				emailProperties.getProperty("mail.port"));
		props.setProperty(emailProperties.getProperty("mail.ssl.property"),
				String.valueOf(true));
		props.setProperty(emailProperties.getProperty("mail.auth.property"),
				String.valueOf(true));

		FileCipherDecipher cipher = new FileCipherDecipher();
		this.password = cipher.getEmailPassword();

	}

	public boolean send() {
		try {
			Authenticator authenticator = new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					return new javax.mail.PasswordAuthentication(username,
							password);
				}
			};

			session = Session.getDefaultInstance(props, authenticator);

			// Create a default MimeMessage object.
			MimeMessage message = new MimeMessage(session);
			// Set From: header field of the header.
			message.setFrom(new InternetAddress(from));
			// Set To: header field of the header.
			for (int i = 0; i < to.length; i++) {
				message.addRecipient(Message.RecipientType.TO,
						new InternetAddress(to[i]));
			}
			// Set Subject: header field
			message.setSubject(subject);
			// Now set the actual message
			message.setText(content);
			Transport transport = session.getTransport("smtps");
			transport.connect(host, port, username, password);
			// Send message
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			return true;
		} catch (MessagingException mex) {
			return false;
		}

	}

	@Override
	public void run() {
		send();
	}

}
